//
//  DataChoose.m
//  HOrangiOS
//
//  Created by YOrange on 2018/4/20.
//  Copyright © 2018年 YOrange. All rights reserved.
//

#import "DataChoose.h"
@interface DataChoose()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (strong, nonatomic) UIPickerView *pickView;

@property (assign, nonatomic) int h;

@property (assign, nonatomic) int w;

///黑色背景
@property (strong, nonatomic) UIView *backView;

///所有组件放置的View
@property (strong, nonatomic) UIView *parentView;

//@property (strong, nonatomic) UIPickerView
@end

@implementation DataChoose{
    NSInteger _year;
    NSInteger _month;
    NSInteger _day;
    NSInteger _chooseYear;
    NSInteger _chooseMonth;
    NSInteger _chooseDay;

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)init{
    if (self = [super init]) {
        
    }
    return self;
}


-(void)configUI{
    self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    _h = self.bounds.size.height;
    _w = self.bounds.size.width;
    //设置黑色背景
    self.backView = [[UIView alloc]initWithFrame:self.bounds];
    [self addSubview:self.backView];
    self.backView.backgroundColor = [UIColor blackColor];
    self.backView.alpha = 0.6;
    
    //父视图
    self.parentView = [[UIView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, _high)];
    self.parentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.parentView];
    //头部
    //左边的按钮
    UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 56, 40)];
    [leftBtn setTitle:@"取消" forState:UIControlStateNormal];
    [leftBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    leftBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [leftBtn addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.parentView addSubview:leftBtn];
    
    //右边的按钮
    UIButton *rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(_w - 56, 0, 56, 40)];
    [rightBtn setTitle:@"确定" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [rightBtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.parentView addSubview:rightBtn];
    
    //选择器
    self.pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 40, _w, _high - 40)];
    self.pickView.delegate = self;
    self.pickView.dataSource = self;
    [self.parentView addSubview:self.pickView];
    
    NSDate *date =[NSDate date];//简书 FlyElephant
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    
    [formatter setDateFormat:@"yyyy"];
    NSInteger currentYear=[[formatter stringFromDate:date] integerValue];
    [formatter setDateFormat:@"MM"];
    NSInteger currentMonth=[[formatter stringFromDate:date]integerValue];
    [formatter setDateFormat:@"dd"];
    NSInteger currentDay=[[formatter stringFromDate:date] integerValue];
    _year = currentYear;
    _month = currentMonth;
    _day = currentDay;
    _chooseYear = _year;
    _chooseMonth = _month;
    _chooseDay = _day;
    //NSLog(@"currentDate = %@ ,year = %ld ,month=%ld, day=%ld",date,currentYear,currentMonth,currentDay);
    

}

#pragma mark - pickerView dataSource
//行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if(_type == Year){
        return 50;
    }
    if (_type == YearAndMonth){
        if (component == 0) {
            return 50;
        }
        return 12;
    }
    if (_type == YearMonthDay){
        if (component == 0) {
            return 50;
        }else if (component == 1){
            return 12;
        }else{
            return [self year:_chooseYear month:_chooseMonth];
        }
        
    }
    return 1;
}

//列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    if(_type == Year){
        return 1;
    }
    if (_type == YearAndMonth){
        return 2;
    }
    if (_type == YearMonthDay){
        return 3;
    }
    return 0;
    
    
}


#pragma mark - pickerView delegate
//  第component列的行高是多少
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 40;
}


//  第component列第row行显示什么文字
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        return [NSString stringWithFormat:@"%ld",_year - 25 + row];
    }else if(component == 1){
        return [NSString stringWithFormat:@"%ld",1 + row];
    }else{
        return [NSString stringWithFormat:@"%ld",1 + row];
    }
    return @"";
}


//  第component列第row行显示怎样的view(内容)
//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;

//  选中了pickerView的第component列第row行

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        _chooseYear = _year - 25 + row;
        [self.pickView reloadAllComponents];
    }else if (component == 1){
        _chooseMonth = 1 + row;
        [self.pickView reloadComponent:2];
    }else{
        _chooseDay = 1 + row;
    }
}

//刷新
-(void)reload{
    [self configUI];
    [self.pickView selectRow:25 inComponent:0 animated:NO];
    [self.pickView selectRow:_month - 1 inComponent:1 animated:NO];
    [self.pickView selectRow:_day - 1 inComponent:2 animated:NO];
    [self.pickView reloadAllComponents];
    
}


-(void)show{
    [[[UIApplication sharedApplication].delegate window] addSubview:self];
    CGRect frame = self.parentView.frame;
    NSLog(@"前%@",self.parentView);
    frame.origin.y -= _high;
    [UIView animateWithDuration:0.2 animations:^{
        
        self.backView.alpha = 0.4;
        self.parentView.frame = frame;
    } completion:^(BOOL finished) {
        NSLog(@"%@",self.parentView);
    }];
    
}

-(void)dismiss{
    CGRect frame = self.parentView.frame;
    frame.origin.y += _high;
    [UIView animateWithDuration:0.2 animations:^{
        self.backView.alpha = 0.4;
        self.parentView.frame = frame;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}

//点击左边的按钮
-(void)leftBtnClick{
    [self dismiss];
    
}

//点击右边的按钮
-(void)rightBtnClick{
    NSLog(@"%ld年%ld月%ld日",(long)_chooseYear,(long)_chooseMonth,(long)_chooseDay);
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    NSString * formatTime;
    if (_type == Year) {
        formatTime = [NSString stringWithFormat:@"%ld",_chooseYear];
        [formatter setDateFormat:@"YYYY"]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制

    }else if (_type == YearAndMonth){
        formatTime = [NSString stringWithFormat:@"%ld-%ld",_chooseYear,_chooseMonth];
        [formatter setDateFormat:@"YYYY-MM"]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制

    }else if (_type == YearMonthDay){
        formatTime = [NSString stringWithFormat:@"%ld-%ld-%ld",_chooseYear,_chooseMonth,_chooseDay];
        [formatter setDateFormat:@"YYYY-MM-dd"]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制

    }
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSDate* date = [formatter dateFromString:formatTime]; //------------将字符串按formatter转成nsdate
    //时间转时间戳的方法:
    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
    NSLog(@"将某个时间转化成 时间戳&&&&&&&timeSp:%ld",(long)timeSp); //时间戳的值
    if (_block) {
        _block([NSString stringWithFormat:@"%ld",timeSp],formatTime);
    }
    [self dismiss];
}

-(NSInteger)year:(NSInteger)y month:(NSInteger)m{
    if (m == 2) {
        if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {
            return 29;
        }else{
            return 28;
        }
    }else if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12){
        return 31;
    }else{
        return 30;
    }
    
}


@end
